// api/session_api/session_update_title.go
package session_api

import (
	"github.com/gin-gonic/gin"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

type SessionUpdateTitleRequest struct {
	SessionID uint   `json:"sessionID" binding:"required"`
	Title     string `json:"title" binding:"required,max=32"`
}

func (SessionApi) SessionUpdateTitleView(c *gin.Context) {
	var cr SessionUpdateTitleRequest
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithError(c, err)
		return
	}

	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)

	var session models.SessionModel
	err = global.DB.Take(&session, "id = ? and user_id = ?", cr.SessionID, claims.UserID).Error
	if err != nil {
		res.FailWithMsg(c, "您的会话不存在")
		return
	}

	global.DB.Model(&session).Update("title", cr.Title)

	res.OkWithMsg(c, "修改会话标题成功")
}
